package com.application.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.application.bean.Items;
import com.application.bean.Menu;
import com.application.bean.MenuConfig;
import com.application.bean.MenuConfigProperty;
import com.application.pojo.MenuMessage;
import com.application.service.Bsm;
import com.google.gson.Gson;

@Controller
public class PageControl {
	@Autowired
	Bsm bsm;
	@Autowired
	MenuConfigProperty m;
	@RequestMapping(value="login",method=RequestMethod.GET)
	public String login(HttpServletRequest  r){
		return "login";
	}
	@RequestMapping(value="home",method=RequestMethod.GET)
	public String webMain(HttpServletRequest  r){
		MenuConfigProperty configProperty = new MenuConfigProperty();
		getMenuMeesage(configProperty);
		Gson gson = new Gson();
		String reuslt = gson.toJson(configProperty);
		System.err.println(reuslt);
		r.setAttribute("configProperty", reuslt);
		r.setAttribute("userName", r.getUserPrincipal().getName());
		return "home";
	}
	@RequestMapping(value="/form/example",method=RequestMethod.GET)
	public String example(HttpServletRequest  r){
		return "form/example";
	}
	@RequestMapping(value="/form/dialog",method=RequestMethod.GET)
	public String dialog(HttpServletRequest  r){
		return "search/example-dialog";
	}
	@RequestMapping(value="/form/grid",method=RequestMethod.GET)
	public String grid(HttpServletRequest  r){
		return "form/grid";
	}
	@RequestMapping(value="/form/message",method=RequestMethod.GET)
	public String message(HttpServletRequest  r,int id){
		r.setAttribute("student", bsm.selectStudentById(id));
		return "search/example";
	}
	@RequestMapping(value="/form/pie",method=RequestMethod.GET)
	public String pie(HttpServletRequest  r){
		int b = bsm.countStudentBoy(),
		    g = bsm.countStudentGirl();
		System.err.println("b="+b+"|||| g="+g);
		r.setAttribute("b", b);
		r.setAttribute("g", g);
		return "chart/pie";
	}
	
	
	/*
	 @RequestMapping(value="index",method=RequestMethod.GET)
	public String index(HttpServletRequest  r){
		return "blocks/index";
	}
	@RequestMapping(value="reports",method=RequestMethod.GET)
	public String reports(HttpServletRequest  r){
		return "blocks/reports";
	}
	@RequestMapping(value="guidely",method=RequestMethod.GET)
	public String guidely(HttpServletRequest  r){
		return "blocks/guidely";
	}
	@RequestMapping(value="charts",method=RequestMethod.GET)
	public String charts(HttpServletRequest  r){
		return "blocks/charts";
	}
	@RequestMapping(value="shortcodes",method=RequestMethod.GET)
	public String shortcodes(HttpServletRequest  r){
		return "blocks/shortcodes";
	}
	@RequestMapping(value="icons",method=RequestMethod.GET)
	public String icons(HttpServletRequest  r){
		return "blocks/icons";
	}
	@RequestMapping(value="faq",method=RequestMethod.GET)
	public String faq(HttpServletRequest  r){
		return "blocks/faq";
	}
	@RequestMapping(value="pricing",method=RequestMethod.GET)
	public String pricing(HttpServletRequest  r){
		return "blocks/pricing";
	}
	@RequestMapping(value="settings",method=RequestMethod.GET)
	public String settings(HttpServletRequest  r){
		String name = r.getParameter("name");
		User u = bsm.selectUserByName(name);
		r.setAttribute("user", u);
		return "blocks/settings";
	}
	 */
	
	private MenuConfigProperty getMenuMeesage(MenuConfigProperty menuConfigProperty){
		//一级菜单
		List<MenuConfig> menuConfigs = new ArrayList<MenuConfig>();
		List<MenuMessage> rootMenuMessages = bsm.selectRoorMenu();
		for (MenuMessage rootMenuMessage : rootMenuMessages) {
			MenuConfig menuConfig = new MenuConfig();
			menuConfig.setId(rootMenuMessage.getMenuId());
			menuConfig.setCollapsed(rootMenuMessage.getCollapsed());
			menuConfig.setHomePage(rootMenuMessage.getHomePage());
			
			//二级菜单
			List<Menu> menus = new ArrayList<Menu>();
			List<MenuMessage> secondMenuMessages = bsm.selectSubMenu(rootMenuMessage.getMenuId());
			for (MenuMessage secondMenuMessage : secondMenuMessages) {
					Menu menu = new Menu();
					menu.setId(secondMenuMessage.getMenuId());
					menu.setText(secondMenuMessage.getText());
					
					//三级菜单
					List<Items> itemss = new ArrayList<Items>();
					List<MenuMessage> thirdMenuMessages = bsm.selectSubMenu(secondMenuMessage.getMenuId());
					for (MenuMessage thirdMenuMessage : thirdMenuMessages) {
						Items items = new Items();
						items.setId(thirdMenuMessage.getMenuId());
						items.setCloseable(thirdMenuMessage.getCloseable());
						items.setHref(thirdMenuMessage.getHref());
						items.setText(thirdMenuMessage.getText());
						itemss.add(items);
						menu.setItems(itemss);
					}
					menus.add(menu);
					menuConfig.setMenu(menus);
			}
			
			menuConfigs.add(menuConfig);
			
		}
		menuConfigProperty.setMenuConfigs(menuConfigs);
		return menuConfigProperty;
		
	}
}
